package com.beans;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.model.DualListModel;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

import com.controllers.RoleController;
import com.model.Action;
import com.model.Activity;
import com.model.Group;
import com.model.Role;
import com.utils.ActionConstants;

@Named
public class DisplayHelperBean implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private RoleController roleController;

	private boolean create;

	private boolean read;

	private boolean update;

	private boolean delete;

	@Inject
	public DisplayHelperBean(RoleController roleController) {
		this.roleController = roleController;
	}

	public void checkAccessBeforeRenderForRole(String myRole) {

		Role role = roleController.getRoleByName(myRole);
		Collection<GrantedAuthority> authorities = SecurityContextHolder
				.getContext().getAuthentication().getAuthorities();
		for (GrantedAuthority grantedAuthority : authorities) {
			Role sessionRole = roleController.getRoleByName(grantedAuthority
					.getAuthority());
			if (sessionRole.getId() == role.getId()) {

				for (Action action : role.getActions()) {
					if (action.getName().equalsIgnoreCase(
							ActionConstants.CREATE.name()))
						create = true;
					if (action.getName().equalsIgnoreCase(
							ActionConstants.READ.name()))
						read = true;
					if (action.getName().equalsIgnoreCase(
							ActionConstants.UPDATE.name()))
						update = true;
					if (action.getName().equalsIgnoreCase(
							ActionConstants.DELETE.name()))
						delete = true;
				}
				return;
			}
		}

	}
	
	public static Activity findSelectedActivity(String activityString,
			List<Activity> activityList) {
		for (Activity activity : activityList) {
			if (activity.toString().equals(activityString)) {
				return activity;
			}
		}
		return null;
	}

	public void validatePickList(FacesContext context, UIComponent component,
			Object pickList) {
		@SuppressWarnings("unchecked")
		DualListModel<Group> groups = (DualListModel<Group>) pickList;
		FacesMessage message = new FacesMessage();
		message.setSeverity(FacesMessage.SEVERITY_ERROR);
		if (groups.getTarget().size() > 1) {
			throw new ValidatorException(message);
		}
	}

	public boolean isCreate() {
		return create;
	}

	public void setCreate(boolean create) {
		this.create = create;
	}

	public boolean isRead() {
		return read;
	}

	public void setRead(boolean read) {
		this.read = read;
	}

	public boolean isUpdate() {
		return update;
	}

	public void setUpdate(boolean update) {
		this.update = update;
	}

	public boolean isDelete() {
		return delete;
	}

	public void setDelete(boolean delete) {
		this.delete = delete;
	}

}
